/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.hiddendanger.controller;

import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.hiddendanger.bean.HiddenDangerRectificationNotice;
import com.koron.hiddendanger.bean.HiddenDangerRectificationReport;
import com.koron.hiddendanger.service.api.HiddenDangerRectificationService;
import com.koron.safetytask.utils.BusinessCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;


@Api(tags = "hiddenDangerRectification", description = "隐患整改通知和报告接口类")
@RestController
@RequestMapping("/hiddenDangerRectification")
public class HiddenDangerRectificationController {
    @Autowired
    private HiddenDangerRectificationService service;
    private final static Logger logger = LoggerFactory.getLogger(HiddenDangerRectificationController.class);

    @ApiOperation("获取隐患整改通知书列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", value = "当前登录人code,pc不传，app必传字段", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "corpCode", value = "水司code", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "checkUnit", value = "检查单位code", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "inspectedUnitName", value = "受检单位", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键词", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页数", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "int", paramType = "query"),
    })
    @RequestMapping(value = "/getNoticeList.htm", method = RequestMethod.GET)
    public MessageBean<DataBean<HiddenDangerRectificationNotice>> queryDangerByCondition(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String userCode, @RequestParam(required = false) final String corpCode,@RequestParam(required = false) final String checkUnit,
                                                                                         @RequestParam(required = false) final String inspectedUnitName,@RequestParam(required = false) String noticeCode, @RequestParam(required = false) String keyword, @RequestParam Integer page, @RequestParam Integer pageSize) {
        MessageBean<DataBean<HiddenDangerRectificationNotice>> msg = new MessageBean<>();
        if (null == user) {
            msg.setCode(BusinessCode.NOT_LOGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return msg;
        }
        Integer start = null;
        if (page == null || page <= 0) {
            page = 1;
            if (pageSize == null || pageSize <= 0) {
                pageSize = 15;
            }
        }
        start = (page <= 1) ? 0 : (page - 1) * pageSize;
        msg = service.getNoticeList(user, corpCode, checkUnit,inspectedUnitName, start, pageSize,keyword,noticeCode);
        if (null != msg.getData()) {
            msg.getData().setPageNo(page);
        }
        return msg;
    }


    @ApiOperation("根据主键查询整改通知书详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "整改通知书id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/noticeDetails.htm", method = RequestMethod.GET)
    public MessageBean<HiddenDangerRectificationNotice> noticeDetails(@RequestParam final String id) {
        return service.getNotice(id);
    }


    @ApiOperation("导出整改通知书，excel版")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "整改通知书id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/exportNoticeByExcel.htm", method = RequestMethod.GET)
    public void exportNoticeByExcel(HttpServletResponse response, @RequestParam(required = false) String id) {
        // 模板
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("static/excel/hiddenDangerRectificationNotice.xlsx");
        XSSFWorkbook workbook = service.exportNoticeByExcel(id, inputStream);
        if (null != workbook) {
            try (OutputStream os = response.getOutputStream()) {
                response.setContentType("application/octet-stream");
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("隐患整改通知书.xlsx", StandardCharsets.UTF_8));
                workbook.write(os);
                os.flush();
            } catch (Exception e) {
                logger.error(e.getMessage(),e);
            }
        }
    }


    @ApiOperation("导出整改通知书，Pdf版本")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "整改通知书id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/exportNotice.htm", method = RequestMethod.GET)
    public void exportNotice(HttpServletResponse response, @RequestParam(required = false) String id) {
        try (OutputStream os = response.getOutputStream()) {
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("隐患整改通知书.pdf", StandardCharsets.UTF_8));
            service.exportNoticeByPdf(id,os);
            os.flush();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }

    @ApiOperation("导出整改报告，excel版本")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "整改报告id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/exportReportByExcel.htm", method = RequestMethod.GET)
    public void exportReportByExcel(HttpServletResponse response, @RequestParam(required = false) String id) {
        // 模板
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("static/excel/hiddenDangerRectificationReport.xlsx");
        XSSFWorkbook workbook = service.exportReportByExcel(id, inputStream);
        if (null != workbook) {
            try (OutputStream os = response.getOutputStream()) {
                response.setContentType("application/octet-stream");
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("隐患整改报告.xlsx", StandardCharsets.UTF_8));
                workbook.write(os);
                os.flush();
            } catch (Exception e) {
                logger.error(e.getMessage(),e);
            }
        }
    }



    @ApiOperation("导出整改报告，pdf版本")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "整改报告id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/exportReport.htm", method = RequestMethod.GET)
    public void exportReport(HttpServletResponse response, @RequestParam(required = false) String id) {
        try (OutputStream os = response.getOutputStream()) {
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("隐患整改报告.pdf", StandardCharsets.UTF_8));
            service.exportReportByPdf(id,os);
            os.flush();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }

    @ApiOperation("获取隐患整改报告列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", value = "当前登录人code,pc不传，app必传字段", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "corpCode", value = "水司code", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "checkUnit", value = "检查单位code", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键词", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页数", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "int", paramType = "query"),
    })
    @RequestMapping(value = "/getReportList.htm", method = RequestMethod.GET)
    public MessageBean<DataBean<HiddenDangerRectificationReport>> queryReportByCondition(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String userCode, @RequestParam(required = false) final String corpCode,
                                                                                         @RequestParam(required = false) final String checkUnit,@RequestParam(required = false) String reportName,@RequestParam(required = false) String keyword,@RequestParam Integer page, @RequestParam Integer pageSize) {
        MessageBean<DataBean<HiddenDangerRectificationReport>> msg = new MessageBean<>();
        if (null == user) {
            msg.setCode(BusinessCode.NOT_LOGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return msg;
        }
        Integer start = null;
        if (page == null || page <= 0) {
            page = 1;
            if (pageSize == null || pageSize <= 0) {
                pageSize = 15;
            }
        }
        start = (page <= 1) ? 0 : (page - 1) * pageSize;
        msg = service.getReportList(user, corpCode,checkUnit, start, pageSize,reportName,keyword);
        if (null != msg.getData()) {
            msg.getData().setPageNo(page);
        }
        return msg;
    }


    @ApiOperation("根据主键查询整改报告详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "整改报告id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/reportDetails.htm", method = RequestMethod.GET)
    public MessageBean<HiddenDangerRectificationReport> reportDetails(@RequestParam final String id) {
        return service.getReport(id);
    }

    @ApiOperation("整改报告照片上传接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "文件所属，1-整改前，2-整改后", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "reportId", value = "整改报告id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "hiddenDangerId", value = "隐患id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/fileImport.htm", method = RequestMethod.POST)
    public MessageBean<?> fileImport(Integer type, String reportId, String hiddenDangerId, MultipartFile[] file) {
        return service.fileImport(type, reportId, hiddenDangerId, file);
    }

    @ApiOperation("整改报告删除照片接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "文件所属，1-整改前，2-整改后", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "reportId", value = "整改报告id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "hiddenDangerId", value = "隐患id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "fileUrl", value = "要删除的附件地址", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/deleteFile.htm", method = RequestMethod.POST)
    public MessageBean<?> deleteFile(Integer type, String reportId, String hiddenDangerId, String fileUrl) {
        return service.deleteFile(type, reportId, hiddenDangerId, fileUrl);
    }

}

